﻿using System.Drawing;
using System.IO;

using Elifart.Library.Serialization;

namespace Elifart.Library
{
    public class AssociationManager
    {
        #region Constructor

        private AssociationManager()
        {
        }

        #endregion

        #region Singleton

        private static AssociationManager _instance;

        public static AssociationManager GetManager()
        {
            if (_instance == null)
                _instance = new AssociationManager();
            return _instance;
        }

        #endregion

        #region Attributes and Properties

        private AssociationCollection _associations;
        public AssociationCollection Associations
        {
            get 
            {
                if (this._associations == null)
                    this._associations = new AssociationCollection();
                return _associations;
            }
        }

        #endregion

        #region Constants

        public const string ASSOCIATIONS_DIRECTORY = @"..\Elifart\Associations";
        public const string ASSOCIATIONS_FILE = "Associations.elf";

        public const string ASSOCIATIONS_BACKUP_DIRECTORY = @"..\Elifart\Backup";
        public const string ASSOCIATIONS_BACKUP_FILE = "Associations_backup.elf";

        public const string ASSOCIATIONS_FLAGS_DIRECTORY = @"..\Elifart\Associations\Flags";

        #endregion

        #region Public Methods

        public bool CreateAssociationsBackup()
        {
            try
            {
                if (!File.Exists(Path.Combine(ASSOCIATIONS_DIRECTORY, ASSOCIATIONS_FILE))) return true;

                if (!Directory.Exists(ASSOCIATIONS_BACKUP_DIRECTORY))
                    Directory.CreateDirectory(ASSOCIATIONS_BACKUP_DIRECTORY);

                File.Copy(Path.Combine(ASSOCIATIONS_DIRECTORY, ASSOCIATIONS_FILE), Path.Combine(ASSOCIATIONS_BACKUP_DIRECTORY, ASSOCIATIONS_BACKUP_FILE), true);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public Image GetAssociationFlag(string imageName)
        {
            string flagFile = Path.Combine(ASSOCIATIONS_FLAGS_DIRECTORY, imageName);
            if (File.Exists(flagFile))
            {
                FileStream stream = new FileStream(flagFile, FileMode.Open);
                Bitmap bitmap = new Bitmap(stream);
                stream.Flush();
                stream.Close();
                return bitmap;
            }
            return null;
        }

        public bool LoadAssociations()
        {
            if (!File.Exists(Path.Combine(ASSOCIATIONS_DIRECTORY, ASSOCIATIONS_FILE)))
                return true;

            try
            {
                AssociationCollection footballAssociationCollection =
                    XamlReader.Load<AssociationCollection>(Path.Combine(ASSOCIATIONS_DIRECTORY, ASSOCIATIONS_FILE));
                this._associations = footballAssociationCollection;
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool LoadAssociationBackup()
        {
            if (!File.Exists(Path.Combine(ASSOCIATIONS_BACKUP_DIRECTORY, ASSOCIATIONS_BACKUP_FILE)))
                return true;

            try
            {
                AssociationCollection footballAssociationCollection =
                    XamlReader.Load<AssociationCollection>(Path.Combine(ASSOCIATIONS_BACKUP_DIRECTORY, ASSOCIATIONS_BACKUP_FILE));
                this._associations = footballAssociationCollection;
                return true;
            }
            catch
            {
                return false;
            }
        }

        public void SaveAssociations()
        {
            if (!Directory.Exists(ASSOCIATIONS_DIRECTORY))
                Directory.CreateDirectory(ASSOCIATIONS_DIRECTORY);
            if (!Directory.Exists(ASSOCIATIONS_FLAGS_DIRECTORY))
                Directory.CreateDirectory(ASSOCIATIONS_FLAGS_DIRECTORY);
            
            XamlWriter.Save(this.Associations, Path.Combine(ASSOCIATIONS_DIRECTORY, ASSOCIATIONS_FILE));
        }

        #endregion
    }
}
